package com.xiaotu.spider.iqy.pageprocessor;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.xiaotu.common.model.MaoYanMoviePeopleModel;
import com.xiaotu.common.model.TVDataSourceRelatetionConditionModel;
import com.xiaotu.common.model.TVDataSourceRelatetionModel;
import com.xiaotu.common.model.TvInfoSpiderTempModel;
import com.xiaotu.common.util.Constants;
import com.xiaotu.common.util.GsonUtils;
import com.xiaotu.common.util.HttpUtils;
import com.xiaotu.common.util.KeywordEncode;
import com.xiaotu.common.util.PageUtils;
import com.xiaotu.common.util.PropertiesUtil;
import com.xiaotu.common.util.StringUtil;
import com.xiaotu.server.service.guduo.SupplementGuDuoRecordService;
import com.xiaotu.server.service.relation.TVDataSourceRelationService;
import com.xiaotu.spider.AUrlPageProcessor;

import us.codecraft.webmagic.Page;
/**
 * 爱奇艺指数相关（地区热度值）
 * @author Mr.zhang
 *
 */
public abstract class IQYIndexPageProcessor extends AUrlPageProcessor{

	protected Map<String,String> headers = null;
	
	{
		headers = new HashMap<String,String>();
	    headers.put("Accept", PropertiesUtil.getProperty("GuDuo.site.Accept"));
	    headers.put("Accept-Encoding", PropertiesUtil.getProperty("GuDuo.site.Accept-Encoding"));
	    headers.put("Accept-Language", PropertiesUtil.getProperty("GuDuo.site.Accept-Language"));
	    headers.put("Cache-Control", PropertiesUtil.getProperty("GuDuo.site.Cache-Control"));
	    headers.put("Connection", PropertiesUtil.getProperty("GuDuo.site.Connection"));
	    headers.put("Cookie", PropertiesUtil.getProperty("GuDuo.site.Cookie"));
	    headers.put("Host", PropertiesUtil.getProperty("GuDuo.site.Host"));
	    headers.put("User-Agent", PropertiesUtil.getProperty("GuDuo.site.User-Agent"));
	}
	
	
	private List<String> areaList = Arrays.asList(new String[]{
				"北京市",
				"天津市",
				"上海市",
				"重庆市",
				"浙江省",
				"江苏省",
				"辽宁省",
				"河北省",
				"吉林省",
				"黑龙江省",
				"广东省",
				"陕西省",
				"福建省",
				"河南省",
				"山东省",
				"山西省",
				"海南省",
				"安徽省",
				"云南省",
				"贵州省",
				"四川省",
				"青海省",
				"湖北省",
				"江西省",
				"甘肃省",
				"湖南省",
				"内蒙古自治区",
				"宁夏回族自治区",
				"西藏自治区",
				"新疆维吾尔自治区",
				"广西壮族自治区",
				"澳门特别行政区",
				"香港特别行政区",
				"台湾",
		});
	
	private List<String> ageList = Arrays.asList(new String[]{
			"1-17",
			"18-24",
			"25-30",
			"31-35",
			"36-40",
			"40+",
	});
	
	
	private List<String> sexList = Arrays.asList(new String[]{
			"帅哥",
			"美女",
	});
	
	private List<String> eduList = Arrays.asList(new String[]{
			"小学",
			"初中",
			"高中-中专",
			"大专",
			"本科",
			"硕士以上"
	});
	
	
	private static final String[] START_URL = new String[]{"https://www.baidu.com/"}; 
	
	protected static final String SEARCH_URL = "https://uaa.if.iqiyi.com/video_index/v2/filtered_suggest_album?key=%s&platform=11&rltnum=100";
	
	//地区热度
	private static final String AREA_HOT_URL = "https://uaa.if.iqiyi.com/video_index/v2/get_province_distribution?album_id=%s";
	
	//人群受众
	private static final String PEOPLE_URL = "https://uaa.if.iqiyi.com/video_index/v2/get_user_profile?album_id=%s";
	
	@Autowired
	private SupplementGuDuoRecordService supplementGuDuoRecordService;
	
	@Override
	public String getPageDataType(Page page) {
		String url = page.getUrl().toString();
		if(url.contains("/filtered_suggest_album?key=")){
			return Constants.PageDataType.SEARCH_LIST;
		}else if(url.contains("/get_province_distribution?album_id")){
			return Constants.PageDataType.TV_HOT;
		}else if(url.contains("/get_user_profile?album_id")){
			return Constants.PageDataType.TV_AUDIENCE;
		}
		return Constants.PageDataType.UNKNOW;
	}
	
	@Override
	public void process(Page page) {
		
		Map<String, Object> extrasMap = page.getRequest().getExtras();

        switch (this.getPageDataType(page))
        {
            case Constants.PageDataType.SEARCH_LIST:
                this.matchNetMovieProcess(page, extrasMap);
                break;
            case Constants.PageDataType.TV_HOT:
                this.netMovieAreaHotProcess(page, extrasMap);
                break;
            case Constants.PageDataType.TV_AUDIENCE:
                this.getNetMoviePeopleProcess(page, extrasMap);
                break;
            case Constants.PageDataType.UNKNOW:
            	this.addBusinessProcess(page, extrasMap);
            	break;
            default:
                break;
        }
	}

	
	protected abstract Integer getTvType();
	
	//爱奇艺人群受众
	@SuppressWarnings({ "rawtypes", "unchecked" })
	private void getNetMoviePeopleProcess(Page page, Map<String, Object> extrasMap) {
		String rawText = page.getRawText();
		
		String url = page.getRequest().getUrl();
		
		String iqy_dataid = extrasMap.get("iqy_dataId").toString();
		
		MaoYanMoviePeopleModel netMoviePeopleModel = (MaoYanMoviePeopleModel)extrasMap.get("netMoviePeopleModel");
		
		String [] sexName = null;
		String [] sexValue = null;
		
		String [] ageName = null;
		String [] ageValue = null;
		
		String [] eduName = null;
		String [] eduValue = null;
		
		
		if(url.contains(iqy_dataid)){
			if(null != rawText && StringUtils.isNotBlank(rawText)){
				Map<String,Object> dataMap = GsonUtils.fromJson(rawText, Map.class);
				List<String> ageLabels = (List<String>)((Map)dataMap.get("data")).get("ageLabels");
				List<Double> ageLabelsValue = (List<Double>)((Map)(((Map)(((List)((Map)dataMap.get("data")).get("details")).get(0))).get(iqy_dataid))).get("age");
				List<String> educationLabels = (List<String>)((Map)dataMap.get("data")).get("educationLabels");
				List<Double> educationLabelsValue = (List<Double>)((Map)(((Map)(((List)((Map)dataMap.get("data")).get("details")).get(0))).get(iqy_dataid))).get("education");
				List<String> genderLabels = (List<String>)((Map)dataMap.get("data")).get("genderLabels");
				List<Double> genderLabelsValue = (List<Double>)((Map)(((Map)(((List)((Map)dataMap.get("data")).get("details")).get(0))).get(iqy_dataid))).get("gender");
				
				ageName = new String[ageList.size()];
				ageValue = new String[ageList.size()];
				eduName = new String[eduList.size()];
				eduValue = new String[eduList.size()];
				sexName = new String[sexList.size()];
				sexValue = new String[sexList.size()];
				for(int i=0; i<ageLabels.size();i++){
					String ageNameStr = ageLabels.get(i);
					if(ageList.indexOf(ageNameStr)!=-1){
						//获取当前位置
						int index = ageList.indexOf(ageNameStr);
						ageName[index] = ageNameStr;
						ageValue[index] = ageLabelsValue.get(i).toString();
					}
				}
				
				for(int i=0;i<educationLabels.size();i++){
					String eduNameStr =educationLabels.get(i);
					if(eduList.indexOf(eduNameStr)!=-1){
						//获取当前位置
						int index = eduList.indexOf(eduNameStr);
						eduName[index] = eduNameStr;
						eduValue[index] = educationLabelsValue.get(i).toString();
					}
				}
				
				for(int i=0;i<genderLabels.size();i++){
					String sexNameStr = genderLabels.get(i);
					if(sexList.indexOf(sexNameStr)!=-1){
						//获取当前位置
						int index = sexList.indexOf(sexNameStr);
						switch (sexNameStr){
							case "帅哥":sexName[index] = "男";
							break;
							case "美女":sexName[index] = "女";
							break;
						}
						
						sexValue[index] = genderLabelsValue.get(i).toString();
					}
				}
				
			}
			
			netMoviePeopleModel.setAgeNames(ageName);
			netMoviePeopleModel.setAgeRates(ageValue);
			netMoviePeopleModel.setEduNames(eduName);
			netMoviePeopleModel.setEduRates(eduValue);
			netMoviePeopleModel.setSexNames(sexName);
			netMoviePeopleModel.setSexRates(sexValue);
			netMoviePeopleModel.setTvType(getTvType());
			//保存
			page.putField(MaoYanMoviePeopleModel.class.getName(), netMoviePeopleModel);
			
		}
		
	}

	//爱奇艺地区热度
	@SuppressWarnings({ "unchecked", "rawtypes" })
	private void netMovieAreaHotProcess(Page page, Map<String, Object> extrasMap) {
		
		String rawText = page.getRawText();
		
		String url = page.getRequest().getUrl();
		
		String [] areaName = null; 
		
		String [] areaValue = null;
		
		double [] areaDoubleValue = null;
		
		
		String iqy_tvName = extrasMap.get("iqy_tvName").toString();
		
		String iqy_dataid = extrasMap.get("iqy_dataId").toString();
		
		if(url.contains(iqy_dataid)){
			
			try {
				if(null != rawText && StringUtils.isNotBlank(rawText)){
					Map<String,Object> dataMap = GsonUtils.fromJson(rawText, Map.class);
					List list = (List)dataMap.get("data");
					if(null != list && list.size()>0){
						List<Map<String,Object>> areaHotList = (List<Map<String,Object>>)((List)dataMap.get("data")).get(0);
						if(null != areaHotList && areaHotList.size()>0){
							areaName = (String[]) areaList.toArray(new String[areaList.size()]);
							areaValue = new String[areaList.size()];
							areaDoubleValue = new double[areaList.size()];
							for(Map<String,Object> areaHotMap : areaHotList){
								
								if(null != areaHotMap.get("name") && StringUtils.isNotBlank(areaHotMap.get("name").toString())){
									if(areaList.indexOf(areaHotMap.get("name").toString())!=-1){
										//获取当前位置
										int index = areaList.indexOf(areaHotMap.get("name").toString());
										areaName[index] = areaHotMap.get("name").toString();
										areaDoubleValue[index] = Double.parseDouble(areaHotMap.get("value").toString());
									}
								}
							}
						}
					}
				}
				
				for(int i = 0;i<areaDoubleValue.length;i++){
					areaValue[i] = areaDoubleValue[i]+"";
				}
			} catch (Exception e) {
				System.out.println(e.getMessage());
			}
			
			MaoYanMoviePeopleModel netMoviePeopleModel = new MaoYanMoviePeopleModel(
					null,iqy_tvName,iqy_dataid,Constants.SiteName.IQIYI,
					null, null,
					null, null, 
					null, null, 
					areaName,areaValue, 
					null, null, 
					null, null);
			extrasMap.put("netMoviePeopleModel", netMoviePeopleModel);
			PageUtils.addTargetRequest(page, String.format(PEOPLE_URL, iqy_dataid), extrasMap);
		}
		
	}

	//匹配爱奇艺网站的剧
	@SuppressWarnings("unchecked")
	private void matchNetMovieProcess(Page page, Map<String, Object> extrasMap) {
		
		String gd_tvName = extrasMap.get("gd_tvName").toString();
		
		String gd_tvDataId = extrasMap.get("gd_tvDataId").toString();
		
		System.out.println("骨朵dataid="+gd_tvDataId);
		System.out.println("骨朵剧名="+gd_tvName);
		
		String rawText = page.getRawText();
		
		String url = page.getRequest().getUrl();
		
		if(KeywordEncode.decode(url).contains(gd_tvName)){
			if(null != rawText && StringUtils.isNotBlank(rawText)){
				
				Map<String,Object> dataMap = GsonUtils.fromJson(rawText, Map.class);
				
				List<Map<String,Object>> netMovieList = (List<Map<String,Object>>)dataMap.get("data");
				
				for(Map<String,Object> netMovieMap : netMovieList){
					
					if((netMovieMap.get("cid").equals("1") && getTvType()==6)//网大
							|| (netMovieMap.get("cid").equals("2") && getTvType()==1) //电视剧
							|| (netMovieMap.get("cid").equals("2") && getTvType()==3)//网剧
							){
						String iqy_tvName = netMovieMap.get("name").toString();
						if(StringUtil.stringFilter(gd_tvName).equals(StringUtil.stringFilter(iqy_tvName))){
							//获取爱奇艺网站剧id
							String iqy_dataid = netMovieMap.get("aid").toString();
							extrasMap.put("iqy_dataId", iqy_dataid);
							extrasMap.put("iqy_tvName", StringUtil.removeBrackets(iqy_tvName));
							//添加地区热度请求
							PageUtils.addTargetRequest(page, String.format(AREA_HOT_URL, iqy_dataid), extrasMap);
							extrasMap.put("filedName", "gd_id");
							extrasMap.put("gu_dataId", gd_tvDataId);
							TVDataSourceRelatetionModel obj = queryTvDataSourceRelationSingle(extrasMap);
							if(null != obj && null == obj.getIqy_id()){
								obj.setIqy_id(iqy_dataid);
								obj.setIqy_name(iqy_tvName);
								updateTVDataSourceRelation(obj);
							}
							break;
						}
					}
					
				}
				
			}
		}
	}
	
	//初始化参数
	protected abstract Map<String,Object> innitParam();

	//获取网大上映日期至今未到210天的
	protected void addBusinessProcess(Page page, Map<String, Object> extrasMap) {
		List<TvInfoSpiderTempModel> list = supplementGuDuoRecordService.queryNotYetAchievedCycleDate(innitParam());
		for(TvInfoSpiderTempModel obj : list){
			if(null != obj.getTvname() && StringUtils.isNotBlank(obj.getTvname())){
				String tvName = obj.getTvname();
				extrasMap = new HashMap<String,Object>();
				if((tvName.lastIndexOf("）")!=-1 || tvName.lastIndexOf(")")!=-1) && (tvName.lastIndexOf("（")!=-1 || tvName.lastIndexOf("(")!=-1)){
					if(tvName.contains("粤语") || tvName.contains("动漫")){
						tvName = StringUtil.removeBrackets(tvName);
					}
				}
				extrasMap.put("gd_tvName", tvName);
				extrasMap.put("gd_tvDataId", obj.getTv_dataid());
				PageUtils.addTargetRequest(page, String.format(SEARCH_URL, KeywordEncode.encode(tvName)), extrasMap);
			}
		}
	}

	@Override
	protected String[] getStartRequestUrls() {
		return START_URL;
	}

	@Override
	protected String getSpiderType() {
		return Constants.SpiderTaskType.TV_IQIYI_INDEX;
	}

	@Autowired
	private TVDataSourceRelationService tVDataSourceRelationService;

	
	//是否已存在剧数据来源关系表中
	public TVDataSourceRelatetionModel queryTvDataSourceRelationSingle(Map<String, Object> extrasMap){
		return tVDataSourceRelationService.queryTvDataSourceRelation(extrasMap);
	}
	
	//新增剧数据来源关系映射表
	public void saveTVDataSourceRelation(TVDataSourceRelatetionModel obj){
		tVDataSourceRelationService.saveTVDataSourceRelation(obj);
	}
	
	//修改剧数据来源关系映射表
	public void updateTVDataSourceRelation(TVDataSourceRelatetionModel obj){
		tVDataSourceRelationService.updateTVDataSourceRelation(obj);
	}
}
